using System;

namespace CSW.Framework.Common.Data
{
    /// <summary>
    /// Converts data returned from a database to .NET data types using DBNull awareness.
    /// </summary>
    public static class DataConverter
    {
        /// <summary>
        /// Gets the value as Int32.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns>The value as Int32.</returns>
        public static int GetInt32(object value)
        {
            return Convert.ToInt32(value);
        }

        /// <summary>
        /// Gets the value as nullable Int32.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns>The value as nullable Int32.</returns>
        public static int? GetNullableInt32(object value)
        {
            if (value.Equals(DBNull.Value)) return null;
            else return Convert.ToInt32(value);
        }

        /// <summary>
        /// Gets the value as Double.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns>The value as Double.</returns>
        public static double GetDouble(object value)
        {
            return Convert.ToDouble(value);
        }

        /// <summary>
        /// Gets the value as nullable Double.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns>The value as nullable Double.</returns>
        public static double? GetNullableDouble(object value)
        {
            if (value.Equals(DBNull.Value)) return null;
            else return Convert.ToDouble(value);
        }

        /// <summary>
        /// Gets the value as String.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns>The value as String.</returns>
        public static string GetString(object value)
        {
            return value.Equals(DBNull.Value) ? string.Empty : value.ToString();
        }

        /// <summary>
        /// Gets the value as Boolean.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns>The value as Boolean.</returns>
        public static bool GetBoolean(object value)
        {
            return (Convert.ToInt32(value) != 0);
        }

        /// <summary>
        /// Gets the value as nullable Boolean.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns>The value as nullable Boolean.</returns>
        public static bool? GetNullableBoolean(object value)
        {
            if (value.Equals(DBNull.Value)) return null;
            else return (Convert.ToInt32(value) != 0);
        }

        /// <summary>
        /// Gets the value as nullable DateTime.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns>The value as nullable DateTime.</returns>
        public static DateTime? GetNullableDateTime(object value)
        {
            if (value.Equals(DBNull.Value)) return null;
            else return Convert.ToDateTime(value);
        }

        /// <summary>
        /// Gets the value as DateTime.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns>The value as DateTime.</returns>
        public static DateTime GetDateTime(object value)
        {
            return Convert.ToDateTime(value);
        }

        /// <summary>
        /// Gets the value as Guid.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns>The value as Guid.</returns>
        public static Guid GetGuid(object value)
        {
            if (value == null || value.Equals(DBNull.Value))
                return Guid.Empty;
            else
                return new Guid(value.ToString());
        }
    }
}
